package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.BaseAttrInfo;
import com.atguigu.gmall.product.entity.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author tangsy
 * @Description TODO
 * @date 2022/11/29 18:08
 * @Version 1.0
 */
@Api(tags = "平台属性")
@RestController
@RequestMapping("admin/product")
public class BaseAttrController {

    @Autowired
    BaseAttrInfoService baseAttrInfoService;

    @Autowired
    BaseAttrValueService baseAttrValueService;

    //admin/product/attrInfoList/{category1Id}/{category2Id}/{category3Id}
    @ApiOperation("根据Id获取分类属性名或值")
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result getBaseAttrList(@PathVariable("category1Id") Long category1Id,
                                  @PathVariable("category2Id") Long category2Id,
                                  @PathVariable("category3Id") Long category3Id){
        List<BaseAttrInfo> attrInfoList = baseAttrInfoService.getAttrAndValue(category1Id,category2Id,category3Id);

        return Result.ok(attrInfoList);
    }

    //api.gmall.com/admin/product/saveAttrInfo
    @ApiOperation("添加平台属性")
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){

        if (baseAttrInfo.getId() != null){
            baseAttrInfoService.updateAttrInfo(baseAttrInfo);
        }else{
            baseAttrInfoService.saveAttrInfo(baseAttrInfo);
        }

        return Result.ok();
    }

    //api.gmall.com/admin/product/getAttrValueList/{attrId}
    @ApiOperation("根据id查询属性值")
    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable("attrId") Long attrId){
        List<BaseAttrValue> attrValueList = baseAttrValueService.getAttrValueList(attrId);
        return Result.ok(attrValueList);
    }

}
